﻿//
// GNS_ManageRecord.cpp
// MobileManageSys
//FuncTion  管理记录
// Created by mr.l on 16/1/14.
//
//
#include "GnsClientFrame.h"
#include "GNS_ManageRecord.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "GNS_PlayerMgr.h"
#include "GNS_Common.h"
USING_NS_CC;
using namespace cocostudio::timeline;
using namespace ui;

#define winsize Director::getInstance()->getVisibleSize()
//extern ISocketMgr * _GameSock;
GNS_ManageRecord::GNS_ManageRecord()
{
    pGNS_ManageRecord=0;
    TimeText=nullptr;
    year=0;
    pCalendar=nullptr;
    pMakeLayer=nullptr;
    pListView=nullptr;
}

GNS_ManageRecord::~GNS_ManageRecord()
{
    
}

GNS_ManageRecord* GNS_ManageRecord::createLayer()
{
    auto pGNS_ManageRecord = GNS_ManageRecord::create();
    auto pScene = Director::getInstance()->getRunningScene();
    if (!pScene)
        return 0;
    pScene->addChild(pGNS_ManageRecord);
    return pGNS_ManageRecord;
}

bool GNS_ManageRecord::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    return true;
}
void GNS_ManageRecord::SetNodeName(std::string CsbName)
{
    pGNS_ManageRecord = CSLoader::createNode(CsbName);
    pGNS_ManageRecord->setContentSize(winsize);
    ui::Helper::doLayout(pGNS_ManageRecord);
    addChild(pGNS_ManageRecord,11);
    
    pMakeLayer = GNS_MakeLayer::CreateMakeLayer("加载中...");
    this->addChild(pMakeLayer,999);
    
    GNS_PlayerMgr::getInstance()->_NodeChildVec.pushBack(pGNS_ManageRecord);
    auto pBackBtn = static_cast<Button*>(pGNS_ManageRecord->getChildByName("ManageRecBackBtn"));
    pBackBtn->setTag(10);
    pBackBtn->addTouchEventListener(CC_CALLBACK_2(GNS_ManageRecord::BtnCallBack, this));
    
    auto pbutton = static_cast<Button *>(pGNS_ManageRecord->getChildByName("Button_1"));
    pbutton->setTag(2);
    pbutton->addTouchEventListener(CC_CALLBACK_2(GNS_ManageRecord::BtnCallBack, this));
    
    auto pTimeSer = static_cast<Button*>(pGNS_ManageRecord->getChildByName("Button_8"));
    pTimeSer->setTag(2);
    pTimeSer->addTouchEventListener(CC_CALLBACK_2(GNS_ManageRecord::BtnCallBack, this));
    
    pListView = static_cast<ListView*>(pGNS_ManageRecord->getChildByName("ListView_1"));
    
    
	time_t tt;
	time(&tt);
	struct tm * time;
	time = localtime(&tt);

	int Mon = (int)time->tm_mon + 1;
	year = time->tm_year + 1900;
	int day = (int)time->tm_mday;
    
    TimeText=static_cast<Text*>(pGNS_ManageRecord->getChildByName("TimeText"));
    TimeText->setString(StringUtils::format("%d-%02d-%02d",year,Mon,day));
    
}
void GNS_ManageRecord::BtnCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        switch (BtnTag) {
            case 10:
            {
                if (GNS_PlayerMgr::getInstance()->_NodeChildVec.size()!=0) {
                    GNS_PlayerMgr::getInstance()->_NodeChildVec.clear();
                    this->removeAllChildrenWithCleanup(true);
                    this->getParent()->removeChild(this);
                }
                //auto SystemNode=GNS_PlayerMgr::getInstance()->_NodeHallVec.at(0);
                //SystemNode->setVisible(true);

				GNS_NodeMgr::getInstance()->setMenuVisible("SystemMgr", true);
                break;
            }
            case 2:
            {
				pCalendar = GNS_Calendar::createCalendar();
				pCalendar->setButtonsCallback(CC_CALLBACK_2(GNS_ManageRecord::TimeButtonCallBack, this));
				/*std::vector<Button *> CalenderBtn;
				pCalendar->GetTimeButton()->swap(CalenderBtn);
				for (auto & it:CalenderBtn) {
				it->setTag(100);
				it->addTouchEventListener(CC_CALLBACK_2(GNS_ManageRecord::TimeButtonCallBack, this));
				}*/
                break;
            }
            default:
                break;
        }
    }
}

void GNS_ManageRecord::TimeButtonCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type)
{
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        std::string BtnText = pButton->getTitleText();
        int Mon = pCalendar->Mon;
        int year = pCalendar->year;
        TimeText->setString(StringUtils::format("%d-%02d-%02d",year,Mon,atoi(BtnText.c_str())));
        pCalendar->Show(false);
        
        std::string SentDate = StringUtils::format("%d-%02d-%02d",year,Mon,atoi(BtnText.c_str()));
        CMD_CS_GetAgentUserManageLog pUserManageLog;
        pUserManageLog.iUserID = (int)GNS_PlayerMgr::getInstance()->getMyUseridx();
        memcpy(pUserManageLog.szLogTime, SentDate.c_str(), SentDate.length() + 1);
        //_GameSock->sendData(MAIN_MSG_LOGIN, SUB_CS_AGENT_MANAGE_LOG, &pUserManageLog, sizeof(pUserManageLog));
        //清空vector
       GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_AGENT_MANAGE_LOG, (char*)&pUserManageLog, sizeof(pUserManageLog));
        GNS_PlayerMgr::getInstance()->getUserManage()->clear();
        pMakeLayer = GNS_MakeLayer::CreateMakeLayer("加载中...");
        this->addChild(pMakeLayer,999);
    }
}
void GNS_ManageRecord::ShowManageDate()
{
    pListView->removeAllItems();
    pMakeLayer->Show(false);
    int x = (int)GNS_PlayerMgr::getInstance()->getUserManage()->size();
    Size bgSize;
    for (auto it:*GNS_PlayerMgr::getInstance()->getUserManage()) {
        auto ItemBg = ImageView::create("line2.png");
        pListView->addChild(ItemBg);
        bgSize = ItemBg->getContentSize();
        
        auto TimeText = Text::create(it.szLogTime, GameFontName, 24);
        TimeText->setPosition(Vec2(bgSize.width*0.14, bgSize.height*0.5));
        TimeText->setColor(Color3B::BLACK);
        ItemBg->addChild(TimeText);
        
        auto OperationText = Text::create(SystemMgr::getInstance()->GbToUTF8(it.szResText), GameFontName, 24);
        OperationText->setTextHorizontalAlignment(TextHAlignment::LEFT);
		OperationText->setTextVerticalAlignment(TextVAlignment::CENTER);
		OperationText->setTextAreaSize(Size(bgSize.width*0.6,bgSize.height));
        OperationText->setAnchorPoint(Vec2(0, 0.5f));
        OperationText->setPosition(Vec2(bgSize.width*0.31, bgSize.height*0.5));  //,,  0.41 ,,
        OperationText->setColor(Color3B::BLACK);
        ItemBg->addChild(OperationText);
    }
    if(bgSize.width!=0||bgSize.height!=0){
        pListView->setInnerContainerSize(Size(bgSize.width,(bgSize.height+10)*x));
    }
}